import Environment

'''
Created on Nov 2, 2009

@author: matthewlepage
'''

class Grid():
    ''' A Grid object tracks the state of all objects in the game space
        and relays this information to the nanobots, as well as controlling
        all GUI information.
    '''
    
    def __init__(self, size):
        self.size = size
        self.objects = []
        
    def validLoc(self, loc):
        ''' Returns True if the location specified by loc
            is a valid location within the confines of the grid;
            False otherwise.
        '''
        row, col = loc
        if not 0 <= row < self.size:
            return False
        if not 0 <= col < self.size:
            return False
        return True
    
    def blockedLoc(self,loc):
        ''' Returns True if their is a location with a True isSolid
            attribute at the given loc; False otherwise.
        '''
        for object in self.objectsAt(loc):
            if object.isSolid():
                return True
        return False
    
    
    def addObj(self, object):
        ''' Adds an object to the gui and grid '''
        self.objects.append(object)

    def getLoc(self, object):
        ''' Gets the location of an object '''
        return object.loc
    
    def objectsAt(self, loc):
        ''' Returns a list of objects at a given location '''
        objects = []
        for obj in self.objects:
            if obj.loc[0] == loc[0] and obj.loc[1] == loc[1]:
                objects.append(obj)
        return objects
    
    def remove(self, object):
        ''' Removes an object from the gui and the grid '''
        self.objects.remove(object)
    
    def locsInRadius(self, loc, radius):
        ''' Returns a list of all the locations within a radius
            of a given location
        '''
        locs = []
        for i in range(0, radius+1):
            for j in range(0, radius+1):
                for (a,b) in ((1,1),(1,-1),(-1,-1),(-1,1),):
                    curLoc =loc[0]+(a*i),loc[1]+(b*j)
                    if self.validLoc(curLoc) and not (curLoc in locs):
                        locs.append(curLoc)
        return locs
    
    def moveObject(self, object, fromLoc, toLoc):
        ''' Moves an object from one location to another '''
        if self.validLoc(toLoc) and not self.objectsAt(toLoc):
            object.loc = toLoc
            return True
        else:
            return False
        
    def isAdjacent(self, locA, locB):
        return abs(locA[0] - locB[0]) < 2 and abs(locA[1] - locB[1]) <2 
            

class GridElement:
    ''' Representation for each square in the grid.
        Contains the square's coordinates, its contents,
        and whether it has been seen or not.
    '''
    def __init__(self, rowcol, seen, timeStamp=0):
        self.loc = rowcol
        self.contents = []
        self.viewed = seen
        self.time = timeStamp
        self.botCount = 0
    
    def __repr__(self):
        return str(self.contents)
    
    def contains(self, type):
        ''' Returns true if the contents of this space contain objects of 
            the given type; false otherwise.'''
        for obj in self.contents:
            if isinstance(obj, type):
                return True
        return False
    
    def get(self, type):
        ''' Gets all elements of the given type in the contents list '''
        for obj in self.contents:
            if isinstance(obj, type):
                return obj
        return None
    
    def copySquare(self):
        ''' Creates another identical GridElement '''
        newSquare = GridElement(self.loc, self.viewed, self.time)
        newSquare.contents = self.contents
        return newSquare
    
def getEmptyGrid(size):
    ''' Creates another two dimensional list of GridElements each with
        empty content lists
    '''
    grid = []
    for i in range(0, size):
        row= []
        for j in range(0, size):
            row.append(GridElement((i,j), False))
        grid.append(row)
    return grid